//
//  LXComposeTextView.swift
//  weibo
//
//  Created by lixin on 16/4/23.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

class LXComposeTextView: UITextView {
    
    //重写父类的属性（字体大小）
    override var font: UIFont?{
    
        didSet{
        
            placeholderLabel.font = font
        }
    }
    
    //占位文字，供外界使用
    var placeholder: String?{
    
        didSet{
        
            placeholderLabel.text = placeholder
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        
        super.init(frame: frame, textContainer: textContainer)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 视图设置
    private func setUI(){
    
        self.backgroundColor = UIColor.whiteColor()
        
        addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(5)
            
            make.top.equalTo(8)
            
            make.width.equalTo(SCREENW - 10)
        }
        
        //监听文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LXComposeTextView.textViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    @objc private func textViewTextDidChange(){
    
        placeholderLabel.hidden = self.hasText()
    }
    
    //MARK: - 懒加载控件
    private lazy var placeholderLabel: UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(12)
        
        label.textColor = UIColor.grayColor()
        
        label.numberOfLines = 0
        
        return label
    }()
    
    //移除通知
    deinit{
    
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}
